﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Text;

namespace ExpertMIS.UserControl
{
    public partial class ucHotHead : System.Web.UI.UserControl
    {
        private DomainBroker _domainBroker = new DomainBroker();
        private AnswerBroker _answerBroker = new AnswerBroker();

        private void RefreshHotQuestion()
        {
            QuestionBroker questionBroker = new QuestionBroker();

            int hotCount = 0;
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("hotquestion.count"),out hotCount))
            {
                hotCount = 10;
            }

            dlHotQuestion.DataSource = questionBroker.GetHotQuestion(hotCount);
            dlHotQuestion.DataBind();
        }

        private int GetReplyCount(string QuestionOID)
        {
            return _answerBroker.GetQuestionAnswerCount(QuestionOID);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshHotQuestion();
        }

        protected void dlHotQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;

                HyperLink hlQuestion = e.Item.FindControl("hlQuestion") as HyperLink;
                if (question != null && hlQuestion != null)
                {
                    hlQuestion.ToolTip =  string.Format("{0}【{1}】" ,question.Title,QuestionBroker.TranslateStatus(question.Status));
                }

                Label lblDomains = e.Item.FindControl("lblDomains") as Label;
                StringBuilder sbDomains = new StringBuilder();
                if (question != null && lblDomains != null)
                {
                    foreach (DomainEntity domain in _domainBroker.GetDomainsByQuerytionOID(question.OID))
                    {
                        sbDomains.Append(string.Format("【<a href='Forums/DomainIntroduce.aspx?oid={0}' target='_self'>{1}</a>】", domain.OID, domain.DomainName));
                    }

                    lblDomains.Text = sbDomains.ToString();
                    if (question.FAQ)
                    {
                        lblDomains.CssClass = "faq";
                    }
                }

                Label lblReplyCount = e.Item.FindControl("lblReplyCount") as Label;
                if (question != null && lblReplyCount != null)
                {
                    lblReplyCount.Text = GetReplyCount(question.OID).ToString();
                }
            }
        }
    }
}